﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using PPAP.Model;
using PPAP.BO;
using System.Configuration;
using System.IO;

namespace PPAP.Web.Part
{
    public partial class FinalDecisions : PageBase
    {
        PPARBO ppbo = new PPARBO();
        T_FileBO manager = new T_FileBO();
        SamplecardBO sbo = new SamplecardBO();
        LogBO lbo = new LogBO();
        MailTemplateBO mbo = new MailTemplateBO();
        PPARTaskBO pbo = new PPARTaskBO();
        public string RID { set; get; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["RID"] != null)
                {
                    RID = Request.QueryString["RID"].ToString();
                    PPAR p = ppbo.GetModel(int.Parse(Request.QueryString["RID"].ToString()));
                    int num = sbo.GetNoPassSample(RID);
                    if (p != null)
                    {
                        //if (p.STATUS == 6)
                        //    btnSubmit.Visible = true;
                        //else
                        //    btnSubmit.Visible = false;
                        txtFinalResultReason.Text = p.FINALRESULTREASON.Trim();
                        txtPartRelatedReason.Text = p.PARTRELATEDRESULTREASON.Trim();
                        txtProcessRelatedReason.Text = p.PROCESSRELATEDRESULTREASON.Trim();
                        txtSubmissionDate.Text = p.RESUBMISSIONDATE == null ? "" : p.RESUBMISSIONDATE <= DateTime.Parse("1900-02-01") ? "" : DateTime.Parse(p.RESUBMISSIONDATE.ToString()).ToString("yyyy-MM-dd");
                        txtValidUntil.Text = p.DEVIATIONEXPIREDATE == null ? "" : p.RESUBMISSIONDATE <= DateTime.Parse("1900-02-01") ? "" : DateTime.Parse(p.DEVIATIONEXPIREDATE.ToString()).ToString("yyyy-MM-dd");
                        if (p.PARTRELATEDRESULT == "R")
                            rbtR1.Checked = true;
                        else if (p.PARTRELATEDRESULT == "Y")
                            rbtY1.Checked = true;
                        else
                            rbtG1.Checked = true;
                        if (p.PROCESSRELATEDRESULT == "R")
                            rbtR2.Checked = true;
                        else if (p.PROCESSRELATEDRESULT == "Y")
                            rbtY2.Checked = true;
                        else
                            rbtG2.Checked = true;
                        if (p.FINALRESULT == "R")
                            rbtR3.Checked = true;
                        else if (p.FINALRESULT == "Y")
                            rbtY3.Checked = true;
                        else
                            rbtG3.Checked = true;
                        rbtG3_CheckedChanged(null, null);
                        if (Request.QueryString["fd"] != null && Request.QueryString["fd"].ToString() == "0")
                        {
                            btnSubmit.Visible = p.FINALSTATUS == 0 ? true : false;
                            btnSave.Visible = p.FINALSTATUS == 0 ? true : false;
                            lblUp.Visible = p.FINALSTATUS == 0 ? true : false;
                            Repeater1.Visible = p.FINALSTATUS == 0 ? true : false;
                            if (p.STATUS != 6||num>0)
                            {
                                btnSubmit.Visible = false;
                                btnSave.Visible = false;
                                lblUp.Visible = false;
                                //
                                rbtG1.Enabled = false;
                                rbtG2.Enabled = false;
                                rbtG3.Enabled = false;
                                rbtR1.Enabled = false;
                                rbtR2.Enabled = false;
                                rbtR3.Enabled = false;
                                rbtY1.Enabled = false;
                                rbtY2.Enabled = false;
                                rbtY3.Enabled = false;
                                txtFinalResultReason.Enabled = false;
                                txtPartRelatedReason.Enabled = false;
                                txtProcessRelatedReason.Enabled = false;
                                txtSubmissionDate.Enabled = false;
                                trdate1.Visible = false;
                                trdate2.Visible = false;
                                txtValidUntil.Enabled = false;
                            }
                        }
                        if (Request.QueryString["fd"] != null && Request.QueryString["fd"].ToString() == "1")
                        {
                            Label9.Text = "Reason for disapproval:";
                            btnApprove.Visible = p.FINALSTATUS == 1 ? true : false;
                            btnReject.Visible = p.FINALSTATUS == 1 ? true : false;
                            rbtG1.Enabled = false;
                            rbtG2.Enabled = false;
                            rbtG3.Enabled = false;
                            rbtR1.Enabled = false;
                            rbtR2.Enabled = false;
                            rbtR3.Enabled = false;
                            rbtY1.Enabled = false;
                            rbtY2.Enabled = false;
                            rbtY3.Enabled = false;
                            txtFinalResultReason.Enabled = false;
                            txtPartRelatedReason.Enabled = false;
                            txtProcessRelatedReason.Enabled = false;
                            txtSubmissionDate.Enabled = false;
                            txtValidUntil.Enabled = false;
                        }
                        if (Request.QueryString["fd"] != null && Request.QueryString["fd"].ToString() == "2")  //qvp
                        {
                            Label9.Text = "Reason for disapproval:";
                            //lblReason.Visible = true;
                            //lblReason.Text = "Reason by BDNT Q-engineer("+p.QUANLITYMANAGER+"):" + p.REASONDISAPPROVALBYQM;
                            btnApprove2.Visible = p.FINALSTATUS == 2 ? true : false;
                            btnReject2.Visible = p.FINALSTATUS == 2 ? true : false;
                            rbtG1.Enabled = false;
                            rbtG2.Enabled = false;
                            rbtG3.Enabled = false;
                            rbtR1.Enabled = false;
                            rbtR2.Enabled = false;
                            rbtR3.Enabled = false;
                            rbtY1.Enabled = false;
                            rbtY2.Enabled = false;
                            rbtY3.Enabled = false;
                            txtFinalResultReason.Enabled = false;
                            txtPartRelatedReason.Enabled = false;
                            txtProcessRelatedReason.Enabled = false;
                            txtSubmissionDate.Enabled = false;
                            txtValidUntil.Enabled = false;
                        }
                        lblUp.Text = "<input type='button' class='button' onclick='javascript:PopUpWindow(\"../Todolist/UploadFile.aspx?RID=" + RID + "&FJID=" + p.DEVIATIONSHEET + "&filetype=Decisions" + "\", 230, 160, 730, 350);'   value='Upload'/>";
                        DataSet ds = manager.GetFileList(p.DEVIATIONSHEET);
                        if (ds.Tables[0].Rows.Count == 0)
                        {
                            txtValidUntil.Enabled = false;
                            trdate2.Visible = false;
                            txtValidUntil.Text = "";
                            DataRow dr = ds.Tables[0].NewRow();
                            dr["F_Filename"] = "<font color='red'>Not upload files</font>";
                            ds.Tables[0].Rows.Add(dr);
                        }
                        else if(Request.QueryString["fd"] != null && Request.QueryString["fd"].ToString() == "0")
                            txtValidUntil.Enabled = true;
                        GridView1.DataSource = ds;
                        GridView1.DataBind();
                       
                        DataSet ds2 = sbo.GetSampleResult(p.ID.ToString());
                        Repeater1.DataSource = ds2;
                        Repeater1.DataBind();
                        GridView3.DataSource = ds2;
                        GridView3.DataBind();
                    }
                    if (btnApprove.Visible || btnApprove2.Visible)
                    {
                        txtReason.Visible = true;
                        Label9.Visible = true;
                    }
                    else
                    {
                        txtReason.Visible = false;
                        Label9.Visible = false;
                    }
                    Repeater1.Visible = false;

                }
            }
            if (Request.QueryString["deid"] != null)
            {
                DeleteFile(Request.QueryString["deid"].ToString());
            }
            if (Request.QueryString["his"] != null && Request.QueryString["his"].ToString() == "1") //从histroy页面跳转过来
            {
                btnApprove.Visible = false;
                btnReject.Visible =  false;
                rbtG1.Enabled = false;
                rbtG2.Enabled = false;
                rbtG3.Enabled = false;
                rbtR1.Enabled = false;
                rbtR2.Enabled = false;
                rbtR3.Enabled = false;
                rbtY1.Enabled = false;
                rbtY2.Enabled = false;
                rbtY3.Enabled = false;
                txtFinalResultReason.Enabled = false;
                txtPartRelatedReason.Enabled = false;
                txtProcessRelatedReason.Enabled = false;
                txtSubmissionDate.Enabled = false;
                txtValidUntil.Enabled = false;
                GridView1.Columns[6].Visible = false;
            }

            if (RID != null && RID != "")
            {
                DataSet dstask = pbo.GetList(" pid=" + RID);
                if (dstask.Tables[0].Rows.Count > 0)
                {
                    GridView2.DataSource = dstask;
                    GridView2.DataBind();
                    GridView2.Visible = true;
                    lblTask.Visible = true;
                }
            }
        }

        public string Status(string status)
        {
            string font = "";
            switch (status)
            {
                case "0":
                    font = "Created";
                    break;
                case "1":
                    font = "Circulating";
                    break;
                case "2":
                    font = "Circulating(<font color='red'>Reject</font>)";
                    break;
                default:
                    font = "Decided";
                    break;
            }
            return font;

        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="fileid"></param>
        private void DeleteFile(string fileid)
        {
            try
            {
                DataSet ds = manager.Getpath(int.Parse(fileid));
                string path = ds.Tables[0].Rows[0]["F_Detail"].ToString();
                
                if (manager.DeleteFile(fileid))
                {
                    // File.Delete(path);
                    DataSet dd = ppbo.GetList(" DEVIATIONSHEET='" + ds.Tables[0].Rows[0]["F_FILEID"].ToString() + "'");

                    lbo.WriteLog(CurrentUser.Id, "Delete file(FinalDecisions):"+ds.Tables[0].Rows[0]["f_filename"].ToString(), dd.Tables[0].Rows[0]["ID"].ToString());

                    AlertRedirect("Deleted successful", "FinalDecisions.aspx?RID=" + dd.Tables[0].Rows[0]["ID"].ToString() );
                    //AlertBack("Deleted successful");
                }

            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["RID"] != null)
            {
                PPAR p = ppbo.GetModel(int.Parse(Request.QueryString["RID"].ToString()));
                if (p != null)
                {
                    if (txtSubmissionDate.Enabled && txtSubmissionDate.Text.Trim() == "")
                    {
                        AlertMessage("Please enter the resubmission date");
                        return;
                    }
                    if (txtValidUntil.Enabled && txtValidUntil.Text.Trim() == "")
                    {
                        AlertMessage("Please enter the expire date");
                        return;
                    }
                    p.PARTRELATEDRESULT = rbtG1.Checked ? "G" : rbtR1.Checked ? "R" : "Y";
                    p.PROCESSRELATEDRESULT = rbtG2.Checked ? "G" : rbtR2.Checked ? "R" : "Y";
                    p.FINALRESULT = rbtG3.Checked ? "G" : rbtR3.Checked ? "R" : "Y";
                    p.PARTRELATEDRESULTREASON = txtPartRelatedReason.Text;
                    p.PROCESSRELATEDRESULTREASON = txtProcessRelatedReason.Text;
                    p.FINALRESULTREASON = txtFinalResultReason.Text;
                    //p.STATUS = 7;
                    p.APPROVALSTATUSBYQM = 0;
                    p.APPROVALSTATUSBYVP = 0;
                    p.RESUBMISSIONDATE = DateTime.Parse(txtSubmissionDate.Text.Trim() == "" ? "1900-01-01" : txtSubmissionDate.Text);
                    p.DEVIATIONEXPIREDATE = DateTime.Parse(txtValidUntil.Text.Trim()==""?"1900-01-01":txtValidUntil.Text);
                    p.FINALSTATUS = 1;
                    //p.SUBMISSIONLEVEL = 1;
                    p.QUANLITYENGINEER = CurrentUser.Id;
                    p.Reportvesion = GetReportNo(p.Reportvesion);  //每次提交，Report版本号加一
                    ppbo.Update(p);

                    WriteTask(p.ID, "Submit by BDNT Q-engineer"); //Write Task


                    //发邮件

                    MailTemplate mail = mbo.GetModel(10);
                        DataSet dsmail = mbo.GetEDM10(p.ID.ToString());
                        if (mail != null && dsmail.Tables[0].Rows.Count > 0)
                        {
                            string title = mail.TITLE.Replace("{Part No.}", dsmail.Tables[0].Rows[0]["bdntpartno"].ToString());
                            title = title.Replace("{Part name}", dsmail.Tables[0].Rows[0]["bdntpartname"].ToString());
                            try
                            {
                                base.SendEmailToUser(dsmail.Tables[0].Rows[0]["email"].ToString(), title, mail.CONTENT,null);
                                lbo.WriteLog(CurrentUser.Id, "Send Email:" + dsmail.Tables[0].Rows[0]["email"].ToString(), Request.QueryString["rid"].ToString());

                            }
                            catch (Exception ex)
                            {
                                AlertMessage(ex.Message.ToString());
                                lbo.WriteLog(CurrentUser.Id, "Send Email:" + ex.Message.ToString(), Request.QueryString["rid"].ToString());
                            }
                        }


                    //发邮件



                    btnSubmit.Visible = false;
                    btnSave.Visible = false;
                    lblUp.Visible = false;
                    lbo.WriteLog(CurrentUser.Id, "Submit (FinalDecisions)", p.ID.ToString());
                    rbtG1.Enabled = false;
                    rbtG2.Enabled = false;
                    rbtG3.Enabled = false;
                    rbtR1.Enabled = false;
                    rbtR2.Enabled = false;
                    rbtR3.Enabled = false;
                    rbtY1.Enabled = false;
                    rbtY2.Enabled = false;
                    rbtY3.Enabled = false;
                    txtFinalResultReason.Enabled = false;
                    txtPartRelatedReason.Enabled = false;
                    txtProcessRelatedReason.Enabled = false;
                    txtSubmissionDate.Enabled = false;
                    txtValidUntil.Enabled = false;
                    lbo.WriteLog(CurrentUser.Id, "Submit (FinalDecisions)", p.ID.ToString());
                    Response.Write("<script language=javascript>top.frames['menu'].location.reload();</script>");
                    Response.Write("<script>top.frames['mainFrame'].location='CreatPPAR.aspx'</script>");
                }
            }
        }

        /// <summary>
        /// 保存流程
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="reamrk"></param>
        private void WriteTask(int? pid,string reamrk)
        {
            //Task
            PPARTask task = new PPARTask();
            task.CREATETIME = DateTime.Now;
            task.CREATEUSER = CurrentUser.Id;
            task.PID = pid;
            task.REAMRK = reamrk;
            pbo.Add(task);
            //
        }

        private string GetReportNo(string oldNo)
        {
            string num = (int.Parse(oldNo) + 1).ToString();
            switch (num.Length)
            {
                case 1:
                    return "0" + num;
                    break;
                default:
                    return num;
                    break;
            }
        }

        /// <summary>
        /// Approve
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnApprove_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["RID"] != null)
            {
                PPAR p = ppbo.GetModel(int.Parse(Request.QueryString["RID"].ToString()));
                if (p != null)
                {
                    p.FINALSTATUS = 2;
                    //p.STATUS = 7;
                    p.QUANLITYMANAGER = CurrentUser.Id;
                    p.APPROVALSTATUSBYQM = 1;
                    p.REASONDISAPPROVALBYQM = txtReason.Text;
                    ppbo.Update(p);

                    WriteTask(p.ID, "Approve by BDNT Q-engineer:"+txtReason.Text); //Write Task

                    //发邮件

                    MailTemplate mail = mbo.GetModel(11);
                    DataSet dsmail = mbo.GetEDM11(p.ID.ToString());
                    DataTable dtemail = new UsersBO().GetVPEmail();  //获取vp EMAIL
                    foreach (DataRow dr in dtemail.Rows)
                    {
                        if (mail != null && dsmail.Tables[0].Rows.Count > 0)
                        {
                            string title = mail.TITLE.Replace("{Part No.}", dsmail.Tables[0].Rows[0]["bdntpartno"].ToString());
                            title = title.Replace("{Part name}", dsmail.Tables[0].Rows[0]["bdntpartname"].ToString());
                            try
                            {
                                base.SendEmailToUser(dr["email"].ToString(), title, mail.CONTENT,null);
                                lbo.WriteLog(CurrentUser.Id, "Send Email:" + dr["email"].ToString(), Request.QueryString["rid"].ToString());

                            }
                            catch (Exception ex)
                            {
                                AlertMessage(ex.Message.ToString());
                                lbo.WriteLog(CurrentUser.Id, "Send Email:" + ex.Message.ToString(), Request.QueryString["rid"].ToString());
                            }
                        }
                    }

                    //发邮件


                    btnApprove.Visible = false;
                    btnReject.Visible = false;
                    lbo.WriteLog(CurrentUser.Id, "Approve (FinalDecisions)", p.ID.ToString());
                    Response.Write("<script language=javascript>top.frames['menu'].location.reload();</script>");
                    Response.Write("<script>top.frames['mainFrame'].location='FDApproval.aspx'</script>");
                }
            }
        }

        /// <summary>
        /// Reject
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnReject_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["RID"] != null)
            {
                PPAR p = ppbo.GetModel(int.Parse(Request.QueryString["RID"].ToString()));
                if (p != null)
                {
                    p.FINALSTATUS = 0;
                    p.APPROVALSTATUSBYQM = 0;
                    p.REASONDISAPPROVALBYQM = txtReason.Text;
                    ppbo.Update(p);
                    WriteTask(p.ID, "Reject by BDNT Q-engineer:" + txtReason.Text); //Write Task
                    btnApprove.Visible = false;
                    btnReject.Visible = false;
                    lbo.WriteLog(CurrentUser.Id, "Reject (FinalDecisions)", p.ID.ToString());
                    Response.Write("<script language=javascript>top.frames['menu'].location.reload();</script>");
                    Response.Write("<script>top.frames['mainFrame'].location='FDApproval.aspx'</script>");
                }
            }
        }

        /// <summary>
        /// ApproveVP
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnApprove2_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["RID"] != null)
            {
                PPAR p = ppbo.GetModel(int.Parse(Request.QueryString["RID"].ToString()));
                if (p != null)
                {
                    p.FINALSTATUS = 3;
                    p.STATUS = 7;
                    p.QUANLITYVP = CurrentUser.Id;
                    p.APPROVALSTATUSBYVP = 1;
                    p.REASONDISAPPROVALBYBP = txtReason.Text;
                    ppbo.Update(p);

                    WriteTask(p.ID, "Approve by BDNT Q-VP:" + txtReason.Text); //Write Task


                    //发邮件

                    MailTemplate mail = null;
                    if (p.FINALRESULT == "G")
                        mail = mbo.GetModel(12);
                    else
                        mail = mbo.GetModel(13);
                    DataSet dsmail = mbo.GetEDM3(p.ID.ToString());
                    if (mail != null && dsmail.Tables[0].Rows.Count > 0)
                    {
                        foreach (DataRow dr in dsmail.Tables[0].Rows)
                        {
                            string title = mail.TITLE.Replace("{Part No.}", dr["bdntpartno"].ToString());
                            title = title.Replace("{Part name}", dr["bdntpartname"].ToString());
                            try
                            {
                                base.SendEmailToUser(dr["username"].ToString(), title, mail.CONTENT,null);
                                lbo.WriteLog(CurrentUser.Id, "Send Email:" + dr["username"].ToString(), Request.QueryString["rid"].ToString());

                            }
                            catch (Exception ex)
                            {
                                AlertMessage(ex.Message.ToString());
                                lbo.WriteLog(CurrentUser.Id, "Send Email:" + ex.Message.ToString(), Request.QueryString["rid"].ToString());
                            }
                        }
                    }


                    //发邮件


                    btnApprove2.Visible = false;
                    btnReject2.Visible = false;
                    lbo.WriteLog(CurrentUser.Id, "Approve (FinalDecisions)", p.ID.ToString());
                    Response.Write("<script language=javascript>top.frames['menu'].location.reload();</script>");
                    Response.Write("<script>top.frames['mainFrame'].location='FDApprovalVP.aspx'</script>");
                }
            }
        }

        /// <summary>
        /// RejectVP
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnReject2_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["RID"] != null)
            {
                PPAR p = ppbo.GetModel(int.Parse(Request.QueryString["RID"].ToString()));
                if (p != null)
                {
                    p.FINALSTATUS = 1;
                    p.APPROVALSTATUSBYVP = 0;
                    p.REASONDISAPPROVALBYBP = txtReason.Text;
                    ppbo.Update(p);

                    WriteTask(p.ID, "Reject by BDNT Q-VP:" + txtReason.Text); //Write Task

                    btnApprove2.Visible = false;
                    btnReject2.Visible = false;
                    lbo.WriteLog(CurrentUser.Id, "Reject (FinalDecisions)", p.ID.ToString());
                    Response.Write("<script language=javascript>top.frames['menu'].location.reload();</script>");
                    Response.Write("<script>top.frames['mainFrame'].location='FDApprovalVP.aspx'</script>");
                }
            }
        }

        /// <summary>
        /// 获取连接
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public string GetLink(string filename)
        {
            //string filepath = "ftp://" + ConfigurationManager.AppSettings["ftpServerIP"] + "/";
            string filepath = ConfigurationManager.AppSettings["FilePath"];
            return filepath + filename;
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            LinkButton link = (LinkButton)sender;
            if (File.Exists(ConfigurationManager.AppSettings["FilePath"] + link.Text))
            {
                FileInfo file = new FileInfo(ConfigurationManager.AppSettings["FilePath"] + link.Text);
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解决中文乱码
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); //解决中文文件名乱码    
                Response.AddHeader("Content-length", file.Length.ToString());
                Response.ContentType = "appliction/octet-stream";
                Response.WriteFile(file.FullName);
                Response.End();
            }
        }

        protected void rbtG3_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtR3.Checked || rbtY3.Checked)
            {
                txtSubmissionDate.Enabled = true;
                trdate1.Visible = true;
            }
            else
            {
                txtSubmissionDate.Enabled = false;
                trdate1.Visible = false;
                txtSubmissionDate.Text = "";
            }
        }

        /// <summary>
        /// 暂存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["RID"] != null)
            {
                PPAR p = ppbo.GetModel(int.Parse(Request.QueryString["RID"].ToString()));
                if (p != null)
                {
                    p.PARTRELATEDRESULT = rbtG1.Checked ? "G" : rbtR1.Checked ? "R" : "Y";
                    p.PROCESSRELATEDRESULT = rbtG2.Checked ? "G" : rbtR2.Checked ? "R" : "Y";
                    p.FINALRESULT = rbtG3.Checked ? "G" : rbtR3.Checked ? "R" : "Y";
                    p.PARTRELATEDRESULTREASON = txtPartRelatedReason.Text;
                    p.PROCESSRELATEDRESULTREASON = txtProcessRelatedReason.Text;
                    p.FINALRESULTREASON = txtFinalResultReason.Text;
                    //p.STATUS = 7;
                    p.APPROVALSTATUSBYQM = 0;
                    p.APPROVALSTATUSBYVP = 0;
                    p.RESUBMISSIONDATE = DateTime.Parse(txtSubmissionDate.Text.Trim() == "" ? "1900-01-01" : txtSubmissionDate.Text);
                    p.DEVIATIONEXPIREDATE = DateTime.Parse(txtValidUntil.Text.Trim() == "" ? "1900-01-01" : txtValidUntil.Text);
                    p.FINALSTATUS = 0;
                    //p.SUBMISSIONLEVEL = 1;
                    p.QUANLITYENGINEER = CurrentUser.Id;
                    //p.Reportvesion = GetReportNo(p.Reportvesion);  //每次提交，Report版本号加一
                   int num= ppbo.Update(p);
                   if (num > 0)
                       AlertMessage("Save succeed!");

                    
                    lbo.WriteLog(CurrentUser.Id, "Save (FinalDecisions)", p.ID.ToString());
                }
            }
        }
    }
}